package com.czb.seckill.rabbitmq;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author: 陈正彬
 * 消息的发送者
 */
@Service
@Slf4j
public class MQSender {

    //装配RabbitTemplate
    @Resource
    private RabbitTemplate rabbitTemplate;

    //方法，发送消息
    public void send(Object msg){
        log.info("发送消息--》" + msg);
        rabbitTemplate.convertAndSend("queue",msg);
    }

    //方法发送消息到交换机
    public void sendFanout(Object msg){
        log.info("发送消息-->"+msg);
        // "" 表示忽略路由
        rabbitTemplate.convertAndSend("fanoutExchange","",msg);
    }

    //方法发送消息到direct交换机,路由queue.red
    public void sendDirect1(Object msg){
        log.info("发送消息-->"+msg);

        rabbitTemplate.convertAndSend("directExchange","queue.red",msg);
    }

    //方法发送消息到direct交换机,路由queue.red
    public void sendDirect2(Object msg){
        log.info("发送消息-->"+msg);

        rabbitTemplate.convertAndSend("directExchange","queue.green",msg);
    }

    //方法发送消息到topic交换机,路由queue.red.message
    public void sendTopic01(Object msg){
        log.info("发送消息-->"+msg);
        rabbitTemplate.convertAndSend("topicExchange","queue.red.message",msg);
    }

    //方法发送消息到topic交换机,路由green.queue.red.message
    public void sendTopic02(Object msg){
        log.info("发送消息-->"+msg);
        rabbitTemplate.convertAndSend("topicExchange","green.queue.red.message",msg);
    }
}
